package net.sourceforge.cruisecontrol.buildloop;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

public class BuildLoopActivator implements BundleActivator {
	public void start(final BundleContext bundleContext) throws Exception {
		Thread thread = new Thread() {
			public void run() {
				while (true) {
					try {
						Thread.sleep(10000);
						ServiceTracker serviceTracker = new ServiceTracker(
								bundleContext, Builder.class.getName(), null);
						serviceTracker.open();
						Object[] builders = (Object[]) serviceTracker
								.getServices();
						if (builders == null || builders.length == 0) {
							System.out.println("There is no builder found.");
						}
						for (Object builder : builders) {
							((Builder) builder).build();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		};
		thread.start();
	}

	public void stop(BundleContext bundleContext) throws Exception {
		System.out.println("do some cleanup task now.");
	}
}
